package com.sxdz.web.goods

import com.sxdz.constant.MessageConstant
import com.sxdz.controller.service.goods.IGoodsBannerService
import com.sxdz.controller.service.goods.IGoodsService
import com.sxdz.models.TGoodsBanner
import com.sxdz.models.TGoodsInfo
import com.sxdz.models.common.JsonResult
import com.sxdz.models.common.Order
import com.sxdz.models.common.RollPage
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * Created by LiYan on 2016/8/4.
 * update by lzw on 2016/8/23
 *
 * 商品信息
 */
@RestController
@CompileStatic
@TypeChecked
@RequestMapping("goods")
class GoodsAction {
    Logger logger = LoggerFactory.getLogger(this.getClass())

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsBannerService goodsBannerService

    /**
     * 获取商品列表
     * @return
     */
    @RequestMapping(value = "getGoods", method = RequestMethod.GET)
    @ResponseBody
    getGoods(@RequestParam(value = "pageNum", defaultValue = "1") String pageNum) {
        def params=[:]
         Integer pageSize=10;
         try {params<<[goodsStatus: "1"]
         params << [goodsModule: "1,4"]
         //List<Map<String, Object>> list = new ArrayList<Map<String, Object>>()
         RollPage rollPage = goodsService.findListByModule(params, Order.desc("create_time"), Integer.parseInt(pageNum), pageSize)
         //获取虚拟商品：1 和 实体商品：2
         //list =goodsService.findListByModule()//获取虚拟商品：1 和 实体商品：2

         logger.info("查询到商品信息")
         return new JsonResult<Object>(MessageConstant.SUCCESS, rollPage)}catch (Exception e){e.printStackTrace();
         return new JsonResult<Object>(MessageConstant.SYS_ERROR, "系统异常")}

    }

    /**
     * 按主键查询goodsInfo
     * @param id
     * @return
     */
    @RequestMapping(value = "getGoodsInfo/{id}", method = RequestMethod.GET)
    @ResponseBody
    getGoodsInfo(@PathVariable("id") Long id) {
        TGoodsInfo goodsInfo = goodsService.selectByPrimaryKey(id)
        if (goodsInfo != null) {
            return new JsonResult<Object>(MessageConstant.SUCCESS, goodsInfo)
        } else {
            return new JsonResult<Object>(MessageConstant.FAIL_QUERY, null);
        }
    }

    /**
     * 按goodsId查询goodsBanner
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "getGoodsBan/{goodsId}", method = RequestMethod.GET)
    @ResponseBody
    getGoodsBanner(@PathVariable("goodsId") Long goodsId) {
        def params = [:]
        params << [goodsId: goodsId]
        List<TGoodsBanner> tGoodsBanner = goodsBannerService.findListByParams(params, null) as List<TGoodsBanner>
        return new JsonResult<Object>(MessageConstant.SUCCESS, tGoodsBanner)
    }

    /**
     * 按parentId查询套餐信息
     * @param parentId
     * @return
     */
    @RequestMapping(value = "getPackage/{parantId}", method = RequestMethod.GET)
    @ResponseBody
    getPackage(@PathVariable("parantId") Long parentId) {
        def params = [:]
        params << [parantId: parentId]
        List<TGoodsInfo> list = new ArrayList<TGoodsInfo>()
        list = goodsService.findListByParams(params)
        return new JsonResult<Object>(MessageConstant.SUCCESS, list)
    }

    /**
     * 查询电视维护费
     * @return
     */
    @RequestMapping(value = "getMaintenance", method = RequestMethod.GET)
    @ResponseBody
    getMaintenance() {
        def params = [:]
        params << [goodsModule: "3"]
        List<TGoodsInfo> list = new ArrayList<TGoodsInfo>()
        list = goodsService.findListByParams(params)
        return new JsonResult<Object>(MessageConstant.SUCCESS, list)
    }
}
